
<template>
  <div>
    <!-- 搜索框 -->
    <Collapse v-model="collapseShow" accordion>
      <Panel name="1">
        <div class="admin-top-right">
          <Button type="primary" @click.stop="addBtn" class="top-right-btn">新增</Button>
          <Button type="error" @click.stop="batchDeletBtn" class="top-right-btn">批量删除</Button>
        </div>
        <div slot="content">
          <Form ref="searchForm" :label-width="100" v-model="condition">
            <Row>
              <Col span="6">
                <FormItem label="课程名">
                  <Input placeholder="请输入课程名" clearable v-model="condition.courseName"></Input>
                </FormItem>
              </Col>
            </Row>
          </Form>
          <!-- 查询重置按钮-->
          <div style="text-align:center">
            <Button type="primary" @click="queryMethod()" class="ivu-btn ivu-btn-primary">查询</Button>
            <Button
              type="info"
              @click="handleResetBtn('condition')"
              class="ivu-btn ivu-btn-dashed"
              style="margin-left: 8px"
            >重置</Button>
          </div>
        </div>
      </Panel>
    </Collapse>
    <!-- 数据表格 -->
    <div>
      <Table
        size="small"
        border
        :stripe="true"
        :columns="tableHeader"
        :data="tableData"
        height="500"
        style="margin-top: 12px"
        @on-selection-change="selectionChange"
      ></Table>
    </div>
    <!-- 增加和修改的模态框 -->
    <div>
      <Modal
        :title="modalTitle"
        :value="modalShow"
        @on-cancel="modelCancle"
        @on-ok="handleSubmit"
        :mask-closable="modalReadonly"
        :width="800"
        ok-text="保存"
      >
        <add-update :row="editForm" :isCourse="true"></add-update>
        <div slot="footer" v-if="footerFlag"></div>
      </Modal>
    </div>
    <!-- 分页组件 -->
    <div>
      <navigation :search.sync="doSearch" @navpage="pagerCallback"></navigation>
    </div>
  </div>
</template>
<script>
import navigation from '@/view/components/navigation/navigation'
import AddUpdate from './AddUpdate.vue'
import httpUtil from '@/libs/httpUtil'

export default {
  name: 'Course',
  components: {
    navigation,
    AddUpdate
  },
  data () {
    return {
      // 表格头
      tableHeader: [
        {
          type: 'selection',
          minwidth: 30,
          align: 'center'
          // fixed: 'left'
        },
        {
          title: '序号',
          type: 'index',
          width: 80,
          align: 'center'
          // fixed: 'left'
        },
        {
          title: '课程名',
          key: 'courseName',
          minWidth: 90,
          ellipsis: true,
          align: 'center'
          // fixed: 'left'
        },
        {
          title: '课程描述',
          key: 'courseDesc',
          minWidth: 90,
          ellipsis: true,
          align: 'center'
          // fixed: 'left'
        },
        {
          title: '创建日期',
          key: 'createdAt',
          minWidth: 110,
          ellipsis: true,
          align: 'center'
        },
        {
          title: '修改日期',
          key: 'updateAt',
          minWidth: 110,
          ellipsis: true,
          align: 'center'
        },
        {
          title: '操作',
          minWidth: 200,
          align: 'center',
          render: (h, params) => {
            let btns = []
            btns.push(
              h(
                'Button',
                {
                  props: {
                    type: 'success',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.modifyBtn(params.row)
                    }
                  }
                },
                '修改'
              )
            )
            btns.push(
              h(
                'Button',
                {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.deleteBtn(params.row.tid)
                    }
                  }
                },
                '删除'
              )
            )
            return h('div', btns)
          }
        }
      ],
      tableData: [], // 列表数据
      collapseShow: false,
      doSearch: false,
      modalTitle: '添加课程',
      modalShow: false,
      modalReadonly: false,
      footerFlag: false,
      editForm: {},
      selectArray: [],
      condition: {}
    }
  },
  methods: {
    // 添加按钮
    addBtn () {
      this.modalTitle = '添加课程'
      // debugger
      this.modalShow = true
      this.editForm = {}
    },

    // 取消添加管理员模态框
    modelCancle () {
      this.modalShow = false
    },

    // 多选
    selectionChange (val) {
      this.selectArray = val
      console.log(val)
      console.log('selctArray:', this.selectArray)
      // debugger
    },

    // 保存
    handleSubmit () {
      httpUtil.asyncPost('admin/pc/course/save', this.editForm, response => {
        if (response.data.code === 200) {
          this.modalShow = false
          this.queryMethod()
          this.$Message.success(response.data.msg)
        } else {
          this.$Message.error(response.data.msg)
        }
      })
    },

    // 修改表格数据
    modifyBtn (row) {
      this.editForm = JSON.parse(JSON.stringify(row)) // 克隆一份，防止修改时，取消了页面值也跟着便
      delete this.editForm.createdAt
      delete this.editForm.updateAt
      delete this.editForm.deletedAt
      this.modalTitle = '修改课程信息'
      this.modalShow = true
    },

    // 删除表格数据
    deleteBtn (id) {
      debugger
      id += ','
      this.handleDelete(id)
    },

    // 批量删除
    batchDeletBtn () {
      let select = this.selectArray
      let ids = ''
      // 判断有无选择
      if (select.length > 0) {
        // 取出每一项id 拼接成字符串
        select.forEach((entity, n) => {
          ids = ids + entity.tid + ','
        })
        this.handleDelete(ids)
      } else {
        this.$Message.warning('请先选择需要删除的选项')
      }
    },

    // 向后端请求删除操作
    handleDelete (ids) {
      httpUtil.asyncDelete(
        'admin/pc/course/delect',
        {
          ids: ids
        },
        res => {
          if (res.data.code === 200) {
            this.queryMethod()
            this.$Message.success(response.data.msg)
          } else {
            this.$Message.error(response.data.msg)
          }
        }
      )
    },

    // 搜索重置
    handleResetBtn () {
      this.$refs['searchForm'].resetFields()
      this.condition = {}
      this.queryMethod()
    },

    // 点击翻页
    pagerCallback (page, search) {
      let option = {
        url: '/admin/pc/course/queryByCondition',
        pageData: page,
        success: response => {
          this.tableData = response.data.records
        }
      }
      if (search) {
        option.params = {
          data: this.condition
        }
      }
      this.$httpUtil.pageSearch(option)
    },
    dateFormat (time) {
      if (time) {
        let date = new Date(time)
        return date.format('yyyy-MM-dd')
      } else {
        return ''
      }
    },
    queryMethod () {
      this.doSearch = true
    }
  },
  mounted: function () {
    this.queryMethod()
  }
}
</script>
